package com.gaowl.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author gaowl@stu.jiangnan.edu.cn
 * @date 2021/11/06/15:47
 */
@Configuration
public class GuliMallCorsConfiguration {

    @Bean // 将跨域配置 加入到容器中
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 配置
        corsConfiguration.addAllowedHeader("*");  // 请求头
        corsConfiguration.addAllowedMethod("*");  // 请求方法
        corsConfiguration.addAllowedOrigin("*");  // 请求的来源
        corsConfiguration.setAllowCredentials(true);  // 是否允许携带cookie，进行跨域
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
